package goreport

import (
	"errors"
	"fmt"
	"strings"
)

// Concatenate r.Err and errSuffix.
func (r Report) Append(errSuffix string) Report {
	r.Err = r.Err + errSuffix

	return r
}

// Convert r.Err to error.
func (r Report) Error() error {
	return errors.New(r.Err)
}

// Return the equality state of r.Err and err.
func (r Report) Is(err string) bool {
	return r.Err == err
}

// Return true if r.Err is empty.
func (r Report) IsNil() bool {
	return r.Err == ""
}

// Concatenate errPrefix and r.Err.
func (r Report) Prepend(errPrefix string) Report {
	r.Err = errPrefix + r.Err

	return r
}

// Analogue of fmt.Print printing r.Err in Output.Fd.
// Thread safety is provided by Output.Mutex.
func (r Report) Print() (int, error) {
	return Print(r.Err)
}

// Analogue of fmt.Print printing r.Err in Output.Fd.
// Thread safety is provided by Output.Mutex.
func (r Report) Printf(format string) (int, error) {
	return Printf(format, r.Err)
}

// Analogue of fmt.Println printing r.Err in Output.Fd.
// Thread safety is provided by Output.Mutex.
func (r Report) Println() (int, error) {
	return Println(r.Err)
}

// Rewrap r.Err in errPrefix.
func (r Report) Rewrap(errPrefix string) Report {
	return r.Unwrap().Wrap(errPrefix)
}

// Wrap r.Err in errPrefix.
func (r Report) Wrap(errPrefix string) Report {
	r.Err = errPrefix + ": " + r.Err

	return r
}

// Wrap r.Err in a formatted specifier.
func (r Report) Wrapf(format string) Report {
	r.Err = fmt.Sprintf(format, r.Err)

	return r
}

// Unwrap r.Err.
func (r Report) Unwrap() Report {
	if i := strings.Index(r.Err, ": "); i != -1 {
		r.Err = r.Err[i+2:]
	}

	return r
}
